<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>排班打卡管理</title>
    <!-- 引入 Semantic UI 样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">

    <!-- 引入 Pikaday 样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css">
    <style>
        .ui.message {
            position: relative;
            padding: 1rem 1.5rem;
            margin-bottom: 20px;
            border-radius: 6px;
            font-size: 1em;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }

        .ui.message .close.icon {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
        }

        /* 全局样式 */
        body {
            background-color: #f7fafc;
            font-family: 'Segoe UI', sans-serif;
        }
        .main-container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 20px;
        }
        /* 搜索模块 */
        .search-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.05);
            padding: 25px;
            margin-bottom: 30px;
        }
        .search-icon {
            color: #646cff;
            font-size: 1.2em;
        }
        /* 卡片式列表 - 优化排班卡片样式 */
        .schedule-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            margin-bottom: 25px;
            padding: 25px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            border-left: 4px solid transparent;
        }
        .schedule-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.12);
        }
        /* 今天的排班卡片特殊样式 */
        .today-schedule {
            border-left-color: #3b82f6;
            background-color: #f8fafc;
        }
        /* 卡片头部优化 */
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #f0f0f0;
            padding-bottom: 15px;
            margin-bottom: 20px;
        }
        .card-header .ui.header {
            margin-bottom: 0;
        }
        .card-header .card-meta {
            color: #6b7280;
            font-size: 0.9em;
            margin-top: 5px;
        }
        /* 状态标签优化 */
        .status-tag {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 0.85em;
            font-weight: 500;
            margin-left: 10px;
            vertical-align: middle;
            transition: all 0.3s ease;
        }
        .status-free {
            background-color: #10b981; /* 绿色 - 空闲 */
            color: white;
        }
        .status-busy {
            background-color: #ef4444; /* 红色 - 处理中 */
            color: white;
        }
        /* 卡片内容优化 */
        .detail-item {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 25px;
            margin-bottom: 25px;
        }
        .detail-item > div {
            background-color: #f9fafb;
            padding: 15px;
            border-radius: 8px;
        }
        .item-label {
            font-weight: 500;
            color: #374151;
            margin-bottom: 8px;
            display: block;
        }
        /* 优化标签和内容的排版 */
        .detail-item > div div {
            margin-bottom: 8px;
            display: flex;
            align-items: center;
        }
        .detail-item > div div i {
            margin-right: 8px;
            color: #646cff;
        }
        /* 分页样式 */
        .pagination-container {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 30px;
        }
        .page-btn {
            margin: 0 5px;
            padding: 6px 12px;
            border-radius: 4px;
            transition: all 0.3s ease;
        }
        .current-page {
            background-color: #646cff;
            color: white;
            font-weight: 600;
        }
        /* 处理按钮样式 - 优化按钮设计 */
        .action-buttons {
            display: flex;
            justify-content: flex-end;
            margin-top: 25px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
        }
        .action-btn {
            margin-left: 15px;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            display: flex;
            align-items: center;
            justify-content: center;
            min-width: 120px;
        }
        .action-btn i {
            margin-right: 8px;
        }
        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
        }
        .btn-check-in {
            background-color: #2196F3;
            color: white;
        }
        .btn-check-in:hover {
            background-color: #1976D2;
        }
        .btn-check-out {
            background-color: #f59e0b;
            color: white;
        }
        .btn-check-out:hover {
            background-color: #d97706;
        }
        .btn-status {
            padding: 8px 16px;
            font-size: 0.9em;
        }
        .btn-free {
            background-color: #10b981;
            color: white;
        }
        .btn-free:hover {
            background-color: #059669;
        }
        .btn-busy {
            background-color: #ef4444;
            color: white;
        }
        .btn-busy:hover {
            background-color: #dc2626;
        }
        .btn-checked-in {
            background-color: #94a3b8; /* 灰色 - 已打卡 */
            color: white;
            cursor: not-allowed;
        }
        .btn-checked-out {
            background-color: #10b981; /* 绿色 - 已下班 */
            color: white;
            cursor: not-allowed;
        }
        /* 添加隐藏样式 */
        .hidden {
            display: none !important;
        }
        /* 自定义动画效果 */
        .fade-in {
            animation: fadeIn 0.3s ease-in-out;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        /* 优化模态框样式 */
        .ui.modal.checkout-modal {
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .ui.modal.checkout-modal > .header {
            border-bottom: 1px solid #f0f0f0;
            padding: 1.5rem 2rem;
            font-size: 1.2rem;
            color: #333;
        }
        .ui.modal.checkout-modal > .content {
            padding: 2rem;
            font-size: 1rem;
            color: #555;
        }
        .ui.modal.checkout-modal > .actions {
            border-top: 1px solid #f0f0f0;
            padding: 1rem 2rem;
            display: flex;
            justify-content: flex-end;
        }
        .ui.modal.checkout-modal .ui.button {
            padding: 0.8rem 1.5rem;
            margin-left: 0.8rem;
            border-radius: 5px;
            font-weight: 500;
        }
        .ui.modal.checkout-modal .ui.button.confirm {
            background-color: #f59e0b;
            color: white;
        }
        .ui.modal.checkout-modal .ui.button.confirm:hover {
            background-color: #d97706;
        }
        .ui.modal.checkout-modal .ui.button.cancel {
            background-color: #f0f0f0;
            color: #333;
        }
        .ui.modal.checkout-modal .ui.button.cancel:hover {
            background-color: #e0e0e0;
        }
        /* 成功提示模态框 */
        .ui.modal.success-modal {
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .ui.modal.success-modal > .header {
            border-bottom: none;
            text-align: center;
            padding: 2rem 2rem 0.5rem;
        }
        .ui.modal.success-modal > .content {
            text-align: center;
            padding: 0.5rem 2rem 2rem;
            font-size: 1rem;
            color: #555;
        }
        .ui.modal.success-modal .success-icon {
            color: #10b981;
            font-size: 3rem;
            margin-bottom: 1rem;
        }
        /* 打卡确认模态框 */
        .ui.modal.checkin-modal {
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .ui.modal.checkin-modal > .header {
            border-bottom: 1px solid #f0f0f0;
            padding: 1.5rem 2rem;
            font-size: 1.2rem;
            color: #333;
        }
        .ui.modal.checkin-modal > .content {
            padding: 2rem;
            font-size: 1rem;
            color: #555;
        }
        .ui.modal.checkin-modal > .actions {
            border-top: 1px solid #f0f0f0;
            padding: 1rem 2rem;
            display: flex;
            justify-content: flex-end;
        }
        .ui.modal.checkin-modal .ui.button {
            padding: 0.8rem 1.5rem;
            margin-left: 0.8rem;
            border-radius: 5px;
            font-weight: 500;
        }
        .ui.modal.checkin-modal .ui.button.confirm {
            background-color: #2196F3;
            color: white;
        }
        .ui.modal.checkin-modal .ui.button.confirm:hover {
            background-color: #1976D2;
        }
        .ui.modal.checkin-modal .ui.button.cancel {
            background-color: #f0f0f0;
            color: #333;
        }
        .ui.modal.checkin-modal .ui.button.cancel:hover {
            background-color: #e0e0e0;
        }
        /* 打卡成功提示模态框 */
        .ui.modal.checkin-success-modal {
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .ui.modal.checkin-success-modal > .header {
            border-bottom: none;
            text-align: center;
            padding: 2rem 2rem 0.5rem;
        }
        .ui.modal.checkin-success-modal > .content {
            text-align: center;
            padding: 0.5rem 2rem 2rem;
            font-size: 1rem;
            color: #555;
        }
        .ui.modal.checkin-success-modal .success-icon {
            color: #2196F3;
            font-size: 3rem;
            margin-bottom: 1rem;
        }
        .ui.message {
            margin: 20px 0;
            padding: 1rem 1.5rem;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            background: linear-gradient(135deg, #f0f9ff, #e0f7ff);
            color: #1a73e8;
            position: relative;
            overflow: hidden;
        }

        .ui.message.positive {
            background: linear-gradient(135deg, #e6fbf0, #ccfae6);
            color: #118d5c;
        }

        .ui.message.negative {
            background: linear-gradient(135deg, #fff0f0, #ffe0e0);
            color: #d93025;
        }

        .ui.message .header i.icon {
            vertical-align: middle;
            margin-right: 8px;
        }
        /* 状态标签优化 */
        .status-tag {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 0.85em;
            font-weight: 500;
            margin-left: 10px;
            vertical-align: middle;
            transition: all 0.3s ease;
        }

        /* 完成 - 绿色 */
        .status-complete {
            background-color: #10b981; /* 绿色 */
            color: white;
        }

        /* 进行中 - 蓝色 */
        .status-in-progress {
            background-color: #3b82f6; /* 蓝色 */
            color: white;
        }

        /* 待处理 - 橙色 */
        .status-pending {
            background-color: #f59e0b; /* 橙色 */
            color: white;
        }

    </style>
</head>
<body>

<div class="main-container">
    <!-- 提示信息区域 -->
    <div th:if="${message}" class="ui message animated fadeIn"
         th:classappend="${message.contains('失败') ? 'negative' : 'positive'}"
         style="animation-duration: 0.5s;">
        <i class="close icon"
           onclick="this.parentElement.style.display='none';"></i>
        <div class="header">
            <i th:class="${message.contains('失败') ? 'times circle icon' : 'check circle outline icon'}"></i>
            <span th:text="${message.contains('失败') ? '操作失败' : '操作成功'}">系统提示</span>
        </div>
        <p th:text="${message}">这里是提示信息</p>
    </div>
    <!-- 搜索区域 -->
    <form class="ui form" th:action="@{/Doctor/DateSchedule}" method="get" id="scheduleSearchForm">
        <div class="search-card">
            <div class="ui grid">
                <div class="row">
                    <!-- 日历输入框 -->
                    <div class="twelve wide column">
                        <div class="ui left icon input">
                            <i class="calendar alternate outline icon link search-icon" id="calendarIcon" style="cursor: pointer;"></i>
                            <input type="text" id="searchDate" name="work_date"
                                   th:value="${searchDate}" placeholder="选择日期" readonly>
                        </div>
                    </div>

                    <!-- 提交按钮 -->
                    <div class="four wide column">
                        <button type="submit" class="ui primary action-btn"
                                style="width: 100%; height: 40px; margin-top: 0;">
                            <i class="search icon"></i> 搜索排班
                        </button>
                    </div>
                </div>
            </div>

            <!-- 隐藏字段：用于 Pikaday 的绑定 -->
            <input type="hidden" id="pikadayField" name="pikadayField">
        </div>
    </form>


    <!-- 排班卡片列表 -->
    <div th:if="${#lists.isEmpty(page.list)}" class="ui info message">
        <i class="info icon"></i>
        <div class="header">提示</div>
        暂无符合条件的排班记录
    </div>

    <div th:each="schedule : ${page.list}"
         class="schedule-card">

        <!-- 卡片头部 -->
        <div class="card-header">
            <div>
                <h4 class="ui header">
                    排班详情：<span th:text="${schedule.id}">1001</span>
                    <div class="card-meta">
                        工作日期：<span th:text="${schedule.work_date}">2025-04-01</span>
                    </div>
                </h4>
            </div>
            <span class="status-tag"
                  th:classappend="${schedule.status == '完成'} ? 'status-complete' : (${schedule.status == '进行中'} ? 'status-in-progress' : (${schedule.status == '待处理'} ? 'status-pending' : ''))"
                  th:text="${schedule.status}">状态</span>
        </div>
        <!-- 卡片内容 -->
        <div class="detail-item">
            <div>
                <div class="item-label">医生信息</div>
                <div><i class="user icon"></i> 医生ID：<span th:text="${schedule.doctor_id}">DOC20250401</span></div>
                <div><i class="user icon"></i> 姓名：<span th:text="${schedule.doctor_name}">张医生</span></div>
            </div>

            <div>
                <div class="item-label">排班信息</div>
                <div><i class="hospital icon"></i> 科室：<span th:text="${schedule.department}">内科</span></div>
                <div><i class="clock icon"></i> 班次：<span th:text="${schedule.shift}">上午班</span></div>
            </div>
        </div>

        <div class="action-buttons">
            <!-- 打卡按钮逻辑 -->
            <a th:if="${schedule.startTime == null}"
               class="action-btn btn-check-in"
               th:href="@{/doctor/checkin(scheduleId=${schedule.id}, TimeButton='checkInBtn')}">
                <i class="calendar check icon"></i> 打卡
            </a>
            <a th:if="${schedule.startTime != null}"
               class="action-btn btn-checked-in"
               href="javascript:void(0);"
               onclick="return false;">
                <i class="calendar check icon"></i> 已打卡
            </a>

            <!-- 签退按钮逻辑 -->
            <a th:if="${schedule.startTime != null and schedule.endTime == null}"
               class="action-btn btn-check-out"
               th:href="@{/doctor/checkin(scheduleId=${schedule.id}, TimeButton='checkOutBtn')}">
                <i class="sign-out alternate icon"></i> 签退
            </a>
            <a th:if="${schedule.endTime != null}"
               class="action-btn btn-checked-out"
               href="javascript:void(0);"
               onclick="return false;">
                <i class="sign-out alternate icon"></i> 已签退
            </a>
        </div>


    </div>

    <!-- 分页组件 -->
    <div class="pagination-container">
        <a th:href="@{/Doctor/checkInSchedule(pageNum=${page.pageNum}-1, searchDate=${searchDate})}"
           class="page-btn basic mini"
           th:unless="${page.isFirstPage}">
            <i class="chevron left icon"></i> 上一页
        </a>

        <span class="page-info" style="margin: 0 15px;">
            第 <strong th:text="${page.pageNum}">1</strong> 页 / 共 <strong th:text="${page.pages}">4</strong> 页 | 总计 <strong th:text="${page.total}">29</strong> 条记录
        </span>

        <a th:href="@{/Doctor/checkInSchedule(pageNum=${page.pageNum}+1, searchDate=${searchDate})}"
           class="page-btn basic mini"
           th:unless="${page.isLastPage}">
            下一页 <i class="chevron right icon"></i>
        </a>

        <form name="pageForm" th:action="@{/Doctor/checkInSchedule}" method="get" style="margin-left: 20px;">
            <input type="hidden" name="searchDate" th:value="${searchDate}">
            <div class="ui mini input">
                <input type="number" class="m-bg" name="pageNum" placeholder="页码"
                       style="width: 50px; height: 30px;" required
                       th:value="${page.pageNum}">
                <button type="submit" class="ui tiny primary button">GO</button>
            </div>
        </form>
    </div>
</div>

</body>
<!-- 引入 jQuery -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- 引入 Semantic UI JS -->
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>

<!-- 引入 Pikaday JS -->
<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>

<script>
    // 初始化日期选择器 - 中文配置
    const dateInput = document.getElementById('searchDate');
    const picker = new Pikaday({
        field: dateInput,
        format: 'YYYY-MM-DD',
        firstDay: 1, // 周一为第一天
        minDate: new Date(2020, 0, 1), // 最小日期
        maxDate: new Date(2030, 11, 31), // 最大日期
        yearRange: [2020, 2030], // 年份范围
        i18n: {
            previousMonth : '上个月',
            nextMonth     : '下个月',
            months        : ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            weekdays      : ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
            weekdaysShort : ['日', '一', '二', '三', '四', '五', '六']
        },
        toString(date, format) {
            // 自定义日期格式化函数，确保中文显示
            const day = date.getDate();
            const month = date.getMonth() + 1;
            const year = date.getFullYear();

            // 确保两位数格式
            return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
        },
        parse(dateString, format) {
            // 解析日期字符串
            const parts = dateString.split('-');
            return new Date(parts[0], parts[1] - 1, parts[2]);
        }
    });

    // 页面加载完成后执行
    $(document).ready(function() {
        // 只在有Thymeleaf传递过来的日期时才设置
        const initialDate = document.getElementById('searchDate').value;
        if (initialDate) {
            picker.setDate(initialDate);
        }

        // 修复日历图标点击事件
        $('#calendarIcon').on('click', function() {
            picker.show();
        });
    });
</script>

</html>
